package com.adam.example.aggregation.bucket;

import com.adam.example.TransportClientHolder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;

import java.net.UnknownHostException;

public class FilterAggregationExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        FilterAggregationBuilder filterAggregationBuilder = AggregationBuilders.filter("agg",
                QueryBuilders.termQuery("title", "java"));
        SearchResponse searchResponse = transportClient.prepareSearch("test_books")
                        .setSize(0).addAggregation(filterAggregationBuilder).get();
        Filter filter = searchResponse.getAggregations().get("agg");
        System.out.println(filter.getDocCount());

        transportClient.close();
    }

}
